package co.yixiang.modules.member.token;


import co.yixiang.common.context.ThreadContextHolder;
import co.yixiang.common.enums.ClientTypeEnum;
import co.yixiang.modules.member.entity.domain.YxUser;
import co.yixiang.modules.security.entity.domain.Token;
import co.yixiang.modules.security.entity.vo.AuthUser;
import co.yixiang.modules.security.enums.UserEnums;
import co.yixiang.modules.security.security.AbstractTokenGenerate;
import co.yixiang.modules.security.security.TokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * 会员token生成
 *
 * @author Chopper
 * @version v4.0
 * @since 2020/11/16 10:50
 */
@Component
public class MemberTokenGenerate extends AbstractTokenGenerate<YxUser> {
    @Autowired
    private TokenUtil tokenUtil;

    @Override
    public Token createToken(YxUser member, Boolean longTerm) {
        //获取客户端类型
        String clientType = ThreadContextHolder.getHttpRequest().getHeader("clientType");
//        ClientTypeEnum clientTypeEnum;
//        try {
//            //如果客户端为空，则缺省值为PC，pc第三方登录时不会传递此参数
//            if (clientType == null) {
//                clientTypeEnum = ClientTypeEnum.PC;
//            } else {
//                clientTypeEnum = ClientTypeEnum.valueOf(clientType);
//            }
//        } catch (IllegalArgumentException e) {
//            clientTypeEnum = ClientTypeEnum.UNKNOWN;
//        }
        //记录最后登录时间，客户端类型
        member.setLastLoginDate(new Date());
        member.setClientEnum(ClientTypeEnum.WECHAT_MP.name());
        // TODO：修改最后登录时间
        // rocketMQTemplate.asyncSend(destination, member, RocketmqSendCallbackBuilder.commonCallback());
        AuthUser authUser = new AuthUser(member.getUsername(), member.getUid(), member.getNickName(), member.getAvatar(), UserEnums.MEMBER);
        //登陆成功生成token
        return tokenUtil.createToken(member.getUsername(), authUser, longTerm, UserEnums.MEMBER);
    }

    @Override
    public Token refreshToken(String refreshToken) {
        return null;
                //tokenUtil.refreshToken(refreshToken, UserEnums.MEMBER);
    }

}
